package org.lex.utils.collections;

/**
 * ʹ�÷��͵�List<T>�洢 double ����ʱ�����¼������⣺ 1��autoboxing/unboxing
 * �����һ����������ʧ 2����List<T>�д洢����Object������,��ʹ�ø����ڴ�
 * 
 * ע�⣺�����<b>����</b>���� Java Collections Framework
 */
public class DoubleList {
	private static final int DEFAULT_INITIAL_CAPACITY = 8;
	/*
	 * The array buffer into which the elements of the data are stored. The
	 * capacity of the DoubleList is the length of this array buffer.
	 */
	private transient double[] data = null;
	/*
	 * The size of the DoubleList (the number of elements it contains).
	 */
	private int size = 0;

	/**
	 * Constructs an empty list with the specified initial capacity.
	 * 
	 * @param initialCapacity
	 *            the initial capacity of the list
	 * @exception IllegalArgumentException
	 *                if the specified initial capacity is negative
	 */
	public DoubleList(int initialCapacity) {
		super();
		if (initialCapacity < 0)
			throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
		this.data = new double[initialCapacity];
	}

	/**
	 * Constructs an empty stack with default initial capacity.
	 */
	public DoubleList() {
		this(DEFAULT_INITIAL_CAPACITY);
	}

	// ====================
	public boolean isEmpty() {
		return 0 == size;
	}

	public void clear() {
		size = 0;
	}

	public int size() {
		return size;
	}

	public double get(int index) {
		if (index >= size)
			throw new IndexOutOfBoundsException(index + " out of " + size);
		return data[index];
	}

	public double set(int index, double value) {
		if (index >= size)
			throw new IndexOutOfBoundsException(index + " out of " + size);
		double old = data[index];
		data[index] = value;
		return old;
	}

	public void add(double value) {
		this.ensureCapacity(size + 1);
		data[size++] = value;
	}

	public double remove(int index) {
		if (index >= size)
			throw new IndexOutOfBoundsException(index + " out of " + size);
		double old = data[index];
		System.arraycopy(data, index + 1, data, index, size - index - 1);
		return old;
	}

	public int indexOf(double value) {
		for (int i = 0; i < size; i++)
			if (data[i] == value)
				return i;
		return -1;
	}

	/**
	 * Increases the capacity of this <tt>ArrayStack</tt> instance, if
	 * necessary, to ensure that it can hold at least the number of elements
	 * specified by the minimum capacity argument.
	 * 
	 * @param minCapacity
	 *            the desired minimum capacity
	 */
	private void ensureCapacity(int minCapacity) {
		if (this.data.length < minCapacity) {
			double[] newData = new double[this.data.length * 2];
			System.arraycopy(this.data, 0, newData, 0, size);
			this.data = newData;
		}
	}

	public String toString() {
		return this.toString(", ");
	}

	public String toString(String sep) {
		StringBuilder buffer = new StringBuilder();
		buffer.append('[');
		for (int i = 0; i < size; i++) {
			buffer.append(String.valueOf(this.data[i]));
			buffer.append(sep);
		}
		if (buffer.length() > sep.length())
			buffer.setLength(buffer.length() - sep.length());
		buffer.append(']');
		return buffer.toString();
	}
}
